/**
 * 
 */
package com.jeecms.wicool.manager.shops.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.shops.RedPacketDao;
import com.jeecms.wicool.entity.shops.Coupon;
import com.jeecms.wicool.entity.shops.RedPacket;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.shops.CouponMng;
import com.jeecms.wicool.manager.shops.RedPacketMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

/**
 * @author spring
 *
 */
@Service
@Transactional
public class RedPacketMngImpl implements RedPacketMng {
	
	@Autowired
	private RedPacketDao redPacketDao;
	
	@Autowired
	private WiShopInfoMng shopMng;
	
	@Autowired
	private CouponMng couponMng;

	@Override
	public Pagination getPage(String name,String startDate,String endDate, int pageNo, int pageSize,CmsUser user) {
		return redPacketDao.getPage(name,startDate,endDate, pageNo, pageSize, user);
	}

	@Override
	public RedPacket findById(Integer id) {
		return redPacketDao.findById(id);
	}

	public RedPacket save(RedPacket redPacket,Integer shopId) {
		ShopInfo shop= shopMng.findById(shopId);
		redPacket.setShop(shop);
		Coupon coupon =  couponMng.findById(redPacket.getCoupon().getId());
		redPacket.setCoupon(coupon);
		redPacketDao.save(redPacket);
		return redPacket;
	}

	public RedPacket update(RedPacket redPacket) {
		ShopInfo shop= shopMng.findById(redPacket.getShop().getId());
		redPacket.setShop(shop);
		Coupon coupon =  couponMng.findById(redPacket.getCoupon().getId());
		redPacket.setCoupon(coupon);
		Updater<RedPacket> updater = new Updater<RedPacket>(redPacket);
		return redPacketDao.updateByUpdater(updater);
	}

	@Override
	public RedPacket deleteById(Integer id) {
		return redPacketDao.deleteById(id);
	}

	@Override
	public RedPacket[] deleteByIds(Integer[] ids) {
		RedPacket[] beans = new RedPacket[ids.length];
		for (int i = 0; i < ids.length; i++) {
			beans[i] = redPacketDao.deleteById(ids[i]);
		}
		return beans;
	}

}
